//
//  RunLoopViewController.m
//  KnowledgeHierarchy
//
//  Created by linyi on 2020/5/18.
//  Copyright © 2020 linyi. All rights reserved.
//  what why how

#import "RunLoopViewController.h"
#import "LinThread.h"
@interface RunLoopViewController ()
@property (weak, nonatomic) IBOutlet UILabel *lable;
@property (weak, nonatomic) IBOutlet UILabel *lable1;
@property (weak, nonatomic) IBOutlet UILabel *lable2;
@property (assign, nonatomic) BOOL finish;
//@property (strong, nonatomic) LinThread *thread; 线程就算全局变量，并且强引用，也不会保活。
@end

@implementation RunLoopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
   
    // Do any additional setup after loading the view.
}

- (void)testRunloop{
     // 主线程的runloop是默认开启的，子线程的runloop不是默认开始的。
    //    NSLog(@"%@",[NSRunLoop currentRunLoop]);
        
        NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerMethod0) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        
        NSTimer *time2 = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerMethod1) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:time2 forMode:UITrackingRunLoopMode];
        
        NSTimer *time3 = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerMethod2) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:time3 forMode:NSRunLoopCommonModes];

        // 如何使用默认模式，不占用 UI 模式；并且不会被 UI 模式所影响；
        // 使用子线程进行 runloop 。
        LinThread *thread = [[LinThread alloc] initWithBlock:^{
            NSTimer *timerThread = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(threadAndTimer) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:timerThread forMode:NSDefaultRunLoopMode];
            
            while (!self->_finish) {
                // 每条线程内部，都有自己的runloop，默认都不开启循环
                [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.00001]]; // 死循环
                // 直接 run 的话，会一直跑，也不释放了。
                [[NSRunLoop currentRunLoop] run];
                // 这个只执行0.0001秒，是一个取巧的设计，每次我只执行这么久，执行完了就是释放了，不一直跑。
                
            }
            
            // 既然死循环，如何释放
            NSLog(@"timerThread 当前线程 %@ ",[NSThread currentThread]);
        }];
        [thread start]; // 子线程
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    _finish = YES;
}

- (void)timerMethod0 {
    static int i = 0; 
    _lable.text = [NSString stringWithFormat:@"默认模式 runloop 定时器次数 %d",i++];
    NSLog(@"当前线程 %@ ",[NSThread currentThread]);
}

- (void)timerMethod1 {
    static int i = 0;
    _lable1.text = [NSString stringWithFormat:@"UI 模式 runloop 定时器次数 %d",i++];
}

- (void)timerMethod2 {
    static int i = 0;
    _lable2.text = [NSString stringWithFormat:@"组合模式 runloop 定时器次数 %d",i++];
}

- (void)threadAndTimer{
    // 子线程直接调用timer 无效
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

